

import HeadPrefab from "../../component/HeadPrefab";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { UserModel } from "../model/UserModel";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewGiveMeili extends ViewBase {
    private searchId: number = 0
    private searchUser: UserModel

    onEnable(): void {
        super.onEnable()
        this.refresh()
    }

    private refresh() {
        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        let lbname: cc.Label = this.getChild(this.node, "lbname", cc.Label)
        let lbid: cc.Label = this.getChild(this.node, "lbid", cc.Label)
        let lbcoins: cc.Label = this.getChild(this.node, "lbcoins", cc.Label)
        lbcoins.string = "最高可赠送" + UserModel.instance.meilis + "点魅力值"
        if (this.searchUser) {
            head.setHead(this.searchUser.userPhoto, this.searchUser.lv)
            lbname.string = this.searchUser.userName
            lbid.string = "ID:" + this.searchUser.userId
        } else {
            lbname.string = ""
            lbid.string = "ID:"
            head.setHead("", 0)
        }
        let txtSearchId: cc.EditBox = this.getChild(this.node, "txtSearchId", cc.EditBox)
        let txtGiveNum: cc.EditBox = this.getChild(this.node, "txtGiveNum", cc.EditBox)
        let btnGive: cc.Node = this.findChild(this.node, "btnGive")
        UIHelper.onAddEvent(btnGive, () => {
            if (txtGiveNum.string == "") {
                UIHelper.showTip("请输入赠送数量")
                return
            }
            let num: number = parseInt(txtGiveNum.string)
            if (num > 0) {
                if (this.searchId > 0) {
                    SocketClient.req_give_meili(this.searchId, num, () => {
                        this.searchUser.meilis += num
                        this.refresh()
                    })
                } else {
                    UIHelper.showTip("请输入赠送id")
                }
            } else {
                UIHelper.showTip("请输入赠送数量")
            }
        })
        let btnSearch: cc.Node = this.findChild(this.node, "btnSearch")
        UIHelper.onAddEvent(btnSearch, () => {
            if (txtSearchId.string == "") {
                UIHelper.showTip("请输入要赠予的玩家ID")
                return
            }
            this.searchId = parseInt(txtSearchId.string)
            if (this.searchId > 0) {
                SocketClient.req_user_info(this.searchId, (user: UserModel) => {
                    this.searchUser = user;
                    this.refresh()
                })
            }
        })
    }

}
